*
* Prepare tuition fee for 2013--2014
*
* Inputs:
*	"${raw_data_tution_${year}}"
* 	"${data_applications_tidy_${year}}"
*
* Output:
*	"${data_dir}/Costly_dominated_choices.dta"
*

forvalues year = 2013/2014 {
	global year `year'
	import delimited "${raw_data_tution_${year}}", stringcols(2) clear
	recast str151 szaknev

	gen szint2 = szint
	drop szint
	gen szint = 1 if szint2 == "Bachelor"
	replace szint = 3 if szint2 == "Undivided"

	gen state_funded2 = state_funded
	drop state_funded
	gen state_funded = state_funded2 == "State-funded"

	gen munkarend2 = munkarend
	drop munkarend
	gen munkarend = 1 if munkarend2 == "Regular"
	replace munkarend = 2 if munkarend2 == "Correspondence"
	replace munkarend = 3 if munkarend2 == "Evening"

	drop szint2 state_funded2 munkarend2

	merge 1:n karkod szaknev szint munkarend state_funded using "${data_applications_tidy_${year}}", ///
		keepusing(id costly_dominated_drop_prog costly_dominated_flip_prog ///
			dominated_dropping_program dominated_flipping_program eligibility ///
			sample_hss any_ba_listed no_funded_version_at_all ///
			costly_dominated_drop_DA_id costly_dominated_drop_id ///
			costly_dominated_flip_DA_id costly_dominated_flip_id ///
			dominated_dropping_applicant_id dominated_flipping_applicant_id)
			
	keep if eligibility == 1 & any_ba_listed == 1 & no_funded_version_at_all == 0

	replace tuition = 0 if _m == 2
	replace semester = 0 if _m == 2
	replace tuition = 0 if dominated_dropping_program == 0 & dominated_flipping_program == 0
	replace semester = 0 if dominated_dropping_program == 0 & dominated_flipping_program == 0

	gen tuition_total = semester * tuition

	collapse (max) tuition_total, by(sample_hss id costly_dominated_drop_DA_id ///
		costly_dominated_drop_id costly_dominated_flip_DA_id costly_dominated_flip_id ///
		dominated_dropping_applicant_id dominated_flipping_applicant_id)
	replace tuition_total = 0 if tuition_total == .

	gen tuition_costly_drop_lower = tuition_total
	replace tuition_costly_drop_lower = 0 if costly_dominated_drop_DA_id != 1
	gen tuition_costly_drop_upper = tuition_total
	replace tuition_costly_drop_upper = 0 if costly_dominated_drop_id != 1
	gen tuition_costly_flip_lower = tuition_total
	replace tuition_costly_flip_lower = 0 if costly_dominated_flip_DA_id != 1
	gen tuition_costly_flip_upper = tuition_total
	replace tuition_costly_flip_upper = 0 if costly_dominated_flip_id != 1

	gen tuition_costly_lower = tuition_total
	replace tuition_costly_lower = 0 if costly_dominated_drop_DA_id != 1 & costly_dominated_flip_DA_id != 1
	gen tuition_costly_upper = tuition_total
	replace tuition_costly_upper = 0 if costly_dominated_drop_id != 1 & costly_dominated_flip_id != 1

	gen dominated_applicant_id = dominated_dropping_applicant_id == 1 | dominated_flipping_applicant_id == 1
	gen costly_dominated_DA_id = costly_dominated_drop_DA_id == 1 | costly_dominated_flip_DA_id == 1
	gen costly_dominated_id = costly_dominated_drop_id == 1 | costly_dominated_flip_id == 1
	
	gen year = `year'
	
	save "${data_dir}/Costly_dominated_choices_${year}.dta", replace
}

use "${data_dir}/Costly_dominated_choices_2013.dta", clear
append using "${data_dir}/Costly_dominated_choices_2014.dta"
save "${data_dir}/Costly_dominated_choices.dta", replace

erase "${data_dir}/Costly_dominated_choices_2013.dta"
erase "${data_dir}/Costly_dominated_choices_2014.dta"
